#include"rotinas.h"

boolean localiza(int id, FILE *fp){
  char lixo;
  int i,idAux,j=1;
  if (id<0) return no;
  fseek(fp, 0, SEEK_SET);
  if((fscanf(fp,"%d",&idAux))<1) return no;
  while(id!=idAux){
    do {
      lixo=fgetc(fp);
    } while(lixo!='\n');
    if((fscanf(fp,"%d",&idAux))<1) return no;
  }
  for(i=id;i/10>0;i=i/10) j++;
  fseek(fp,(-1)*j,SEEK_CUR);
  return yes;
}

int compara_data(int data1, int data2){
	int num[8], i, dia1, mes1, ano1, dia2, mes2, ano2;
	for(i=7; i>=0; i--){
		num[i] = data1%10;
		data1 -= num[i];
		data1 = data1/10;
	}
	dia1 = num[0]*10+num[1];
	mes1 = num[2]*10+num[3];
	ano1 = num[4]*1000+num[5]*100+num[6]*10+num[7];

	for(i=7; i>=0; i--){
		num[i] = data2%10;
		data2 -= num[i];
		data2 = data2/10;
	}
	dia2 = num[0]*10+num[1];
	mes2 = num[2]*10+num[3];
	ano2 = num[4]*1000+num[5]*100+num[6]*10+num[7];

	if(ano1 > ano2) return 1;
	if(ano1 < ano2) return -1;
	if(mes1 > mes2) return 1;
	if(mes1 < mes2) return -1;
	if(dia1 > dia2) return 1;
	if(dia1 < dia2) return -1;
	return 0;
}

void ler_frase_arq(char * field, int tamanho, FILE * arq){
	int i;
	char ch1, ch2;
	for(i=0; i<tamanho-1; i++){
		ch1 = fgetc(arq);
		if(ch1 == '\n'){
			field[i] = '\0';
			return;
		}
		else if(ch1 == ' '){
			ch2 = fgetc(arq);
			if(ch2 == '#'){
				field[i] = '\0';
				return;
			}
			else{
				field[i] = ch1;
				field[i+1] = ch2;
				i++;
			}
		}
		else{
			field[i] = ch1;
		}
	}
	return;
}


void *pulacampo(int a, FILE *fp){
     char lixo;
     int i;
     
     for(i=0;i<a;i++){
                      do{
                         lixo = fgetc(fp);
                         }while(lixo != '|');
                         }
      lixo = fgetc(fp);
      return (void*)fp;
      }

int isdiv(char a){ //is_divisor
     if((a == '|') || (a == EOF)) return 1;
     else return 0;
     }

void* pulaLinha(FILE *fp){
     char lixo;
     
     while( (!isdigit(lixo = fgetc(fp))) && (lixo == EOF) ) ;
     fseek ( fp , -1 , SEEK_CUR );
     return (FILE*)fp;
     } 
                         
